
var app = getApp();
var lru='';
Page({
  data: {
  type:'',
  CARD:'',
  zhanghao: '',
  mima: '',
  name:'',
  sex:'',
  IDcard:'',
  fileID:'',
  houseID:'',
  ioNumber:'',
  },
  chooseimage: function () {
    var _this = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        _this.setData({
          fileID:res.tempFilePaths
        })
      }
    })
  },
  //获取用户名
  getIoNumber(event) {
    console.log('获取输入的出入次数', event.detail.value)
    this.setData({
     ioNumber: event.detail.value
    })
    },
    getName(event) {
      console.log('获取输入的姓名', event.detail.value)
      this.setData({
       name: event.detail.value
      })
      },
      getSex(event) {
        console.log('获取输入的性别', event.detail.value)
        this.setData({
         sex: event.detail.value
        })
        },
        getIDcard(event) {
          console.log('获取输入的身份证号', event.detail.value)
          this.setData({
           IDcard: event.detail.value
          })
          },
          getHouseID(event) {
            console.log('获取输入的住户房号', event.detail.value)
            this.setData({
             houseID: event.detail.value
            })
            },
  //获取输入的账号
  getZhanghao(event) {
  //console.log('账号', event.detail.value)
  this.setData({
   zhanghao: event.detail.value
  })
  
  },
  //获取输入的密码
  getMiMa(event) {
  // console.log('密码', event.detail.value)
  this.setData({
   mima: event.detail.value
  })
  },
  tj(){
    let fileID = this.data.fileID
    let name = this.data.name
    let sex = this.data.sex
    let IDcard = this.data.IDcard
    let houseID = this.data.houseID
    let ioNumber = this.data.ioNumber
    app.globalData.fileID=fileID
    app.globalData.name=name
    app.globalData.sex=sex
    app.globalData.IDcard=IDcard
    app.globalData.houseID=houseID
    app.globalData.ioNumber=ioNumber
    wx.navigateTo({
      url: '../bg/bg',
     })

  },
  //点击登陆
  login() {
  let zhanghao = this.data.zhanghao
  let mima = this.data.mima
  console.log('账号', zhanghao, '密码', mima)
  if (zhanghao.length < 4) {
    wx.showToast({
     icon: 'none',
     title: '账号至少4位',
    })
    return
   }
  if (mima.length < 4) {
   wx.showToast({
    icon: 'none',
    title: '密码至少4位',
   })
   return
  }
  
  //登陆
  wx.cloud.database().collection('user').where({
   zhanghao: zhanghao
  }).get({
   success(res) {
    console.log("获取数据成功", res)
    let user = res.data[0]
    console.log("user", user)
    if (mima == user.mima) {
     console.log('登陆成功')
     wx.showToast({
      title: '登陆成功',
     })
     // wx.navigateTo({
     //  url: '../home/home?name=' + user.name,
     // })
  
     app.globalData.type=user.type
     app.globalData.CARD=user.CARD
     app.globalData.name=user.name
     app.globalData.sex=user.sex
     app.globalData.IDcard=user.IDcard
     app.globalData.fileID=user.fileID
     app.globalData.houseID=user.houseID
     app.globalData.ioNumber=user.ioNumber
     console.log(app.globalData.mima);
      wx.switchTab({
        url: '../list/list',
      })
     //保存用户登陆状态
     wx.setStorageSync('user', user)
    } else {
     console.log('登陆失败')
     wx.showToast({
      icon: 'none',
      title: '账号或密码不正确',
     })
    }
   },
   fail(res) {
    console.log("获取数据失败", res)
   }
  })
  
  }
  })